home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / netuser.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  3KB  |  158 lines

  1. /* Miscellaneous format conversion subroutines */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "netuser.h"
  7. #ifdef UNIX
  8. #include "unix.h"
  9. #include <string.h>
  10. #endif /* UNIX */
  11. #ifdef    BSD
  12. char *sprintf();
  13. #endif
  14.  
  15. int net_error;
  16.  
  17. #define LINELEN 256
  18.  
  19. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  20.  * binary IP address
  21.  */
  22. int32
  23. aton(s)
  24. register char *s;
  25. {
  26.     int32 n;
  27.     int atoi();
  28.     register int i;
  29.  
  30.     n = 0;
  31.     for(i=24;i>=0;i -= 8){
  32.         n |= (int32)atoi(s) << i;
  33.         if((s = index(s,'.')) == NULLCHAR)
  34.         break;
  35.         s++;
  36.     }
  37.     return n;
  38. }
  39. /* Resolve a host name into an IP address. IP addresses in dotted-decimal
  40.  * notation are distinguished from domain names by enclosing them in
  41.  * brackets, e.g., [44.64.0.1]
  42.  */
  43. int32
  44. resolve(host)
  45. char *host;
  46. {
  47.     register char *cp,*cp1;
  48.     int i;
  49.     char hostent[LINELEN];
  50.     FILE *sfile;
  51.     static struct {
  52.         char *name;
  53.         int32 address;
  54.     } cache;
  55.  
  56.     if(*host == '['){
  57.         /* Brackets indicate IP address in dotted-decimal form */
  58.         return aton(host + 1);
  59.     }
  60.     if(cache.name != NULLCHAR && strcmp(cache.name,host) == 0)
  61.         return cache.address;
  62.  
  63.     /* Not a numerical IP address, search the host table */
  64.     if((sfile = fopen(hosts,"r")) == NULL){
  65.         return 0;
  66.     }
  67.     while (!feof(sfile)){
  68.         fgets(hostent,LINELEN,sfile);
  69.         rip(hostent);
  70.         cp = hostent;
  71.         if(*cp == '#' || !isdigit(*cp))
  72.             continue;    /* Comment or invalid line */
  73.         while(cp != NULLCHAR){
  74.             /* Skip white space */
  75.             while(*cp == ' ' || *cp == '\t')
  76.                 cp++;
  77.             if(*cp == '\0')
  78.                 break;
  79.             /* Look for next token, find length of this one */
  80.             if((cp1 = index(cp,'\t')) != NULLCHAR){
  81.                 i = cp1 - cp;
  82.             } else if((cp1 = index(cp,' ')) != NULLCHAR) {
  83.                 i = cp1 - cp;
  84.             } else {
  85.                 i = strlen(cp);
  86.             }
  87.             if(strlen(host) == i && strncasecmp(host,cp,i) == 0){
  88.                 /* Found it, put in cache */
  89.                 fclose(sfile);
  90.                 if(cache.name != NULLCHAR)
  91.                     free(cache.name);
  92.                 cache.name = malloc((unsigned)strlen(host)+1);
  93.                 strcpy(cache.name,host);
  94.                 cache.address = aton(hostent);
  95.                 return cache.address;
  96.             }
  97.             /* That one didn't match, try the next one */
  98.             cp = cp1;
  99.         }
  100.     }
  101.     /* No address found */
  102.     fclose(sfile);
  103.     return 0;
  104. }
  105.  
  106. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  107.  * string, e.g., 255.255.255.255\0
  108.  */
  109. char *
  110. inet_ntoa(a)
  111. int32 a;
  112. {
  113.     static char buf[16];
  114.  
  115.     sprintf(buf,"%u.%u.%u.%u",
  116.         hibyte(hiword(a)),
  117.         lobyte(hiword(a)),
  118.         hibyte(loword(a)),
  119.         lobyte(loword(a)) );
  120.     return buf;
  121. }
  122. /* Convert a socket (address + port) to an ascii string of the form
  123.  * aaa.aaa.aaa.aaa:ppppp
  124.  */
  125. char *
  126. psocket(s)
  127. struct socket *s;
  128. {
  129.     static char buf[30];
  130.  
  131.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  132.     return buf;
  133. }
  134. /* Convert hex-ascii string to long integer */
  135. long
  136. htol(s)
  137. char *s;
  138. {
  139.     long ret;
  140.     char c;
  141.  
  142.     ret = 0;
  143.     while((c = *s++) != '\0'){
  144. #if    (!ATARI_ST && !LATTICE)    /* DG2KK "ik versta er heelemal niets van!" */
  145.         c &= 0x7f;
  146. #endif
  147.         if(c >= '0' && c <= '9')
  148.             ret = ret*16 + (c - '0');
  149.         else if(c >= 'a' && c <= 'f')
  150.             ret = ret*16 + (10 + c - 'a');
  151.         else if(c >= 'A' && c <= 'F')
  152.             ret = ret*16 + (10 + c - 'A');
  153.         else
  154.             break;
  155.     }
  156.     return ret;
  157. }
  158.